package com.ds.lens.data.common.client.vo;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import org.springframework.util.CollectionUtils;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author WeiShaoying
 * @date 2019/11/8 下午2:46
 */
@Setter
@Getter
public class LensMetricInVO {

    @NotNull(message = "集成id不能为空")
    private Long integrationId;

    @NotNull(message = "开始时间不能为空")
    private Long startTime;

    @NotNull(message = "结束时间不能为空")
    private Long endTime;

    @NotNull(message = "时间间隔不能为空")
    private Long interval;

    private Map<String, Object> scope;

    @NotEmpty(message = "指标名称不能为空")
    private String metricName;

    private Map<String, Object> metricFilter;

    @NotNull(message = "聚合类型不能为空")
    private Integer metricAggr;

    private List<String> metricBy;

    @NotNull(message = "appId不能为空")
    private Long appId;

    private Integer groupLimit;

    private Integer detectionMethod; // 1 static, 2 dynamic, if missing means 1

    private Map<String, Object> thresholdLevel; //动态告警所用方差和告警级别对应关系

    private List<Integer> devRatio;  //动态告警所用方差

    public LensMetricInVO() {
    }

    @Builder
    public LensMetricInVO(Long integrationId, Long startTime, Long endTime, Long interval, Map<String, Object> scope,
                          String metricName, Map<String, Object> metricFilter, Integer metricAggr, List<String> metricBy,
                          Long appId, Integer groupLimit, Integer detectionMethod, Map<String, Object> thresholdLevel) {
        this.integrationId = integrationId;
        this.startTime = startTime;
        this.endTime = endTime;
        this.interval = interval;
        this.scope = scope;
        this.metricName = metricName;
        this.metricFilter = metricFilter;
        this.metricAggr = metricAggr;
        this.metricBy = metricBy;
        this.appId = appId;
        this.groupLimit = groupLimit;
        this.detectionMethod = detectionMethod;
        setThresholdLevel(thresholdLevel);
    }

    public void setThresholdLevel(Map<String, Object> thresholdLevel) {
        this.thresholdLevel = thresholdLevel;
        devRatio = new ArrayList<>();
        if (!CollectionUtils.isEmpty(thresholdLevel)) {
            for (String key : thresholdLevel.keySet()) {
                devRatio.add(Integer.valueOf(thresholdLevel.get(key).toString()));
            }
            Collections.sort(this.devRatio, (o1, o2) -> {
                if (o1 < o2) {
                    return 1;
                } else if (o1 > o2) {
                    return -1;
                } else {
                    return 0;
                }
            });
        }
    }
}
